package com.jeff.tianti.controller.channel;

import com.jeff.tianti.cms.dto.ActivityAlertsQueryDTO;
import com.jeff.tianti.cms.entity.ActivityAlerts;
import com.jeff.tianti.cms.service.ActivityAlertsService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
@RequestMapping("/channel/activityAlerts")//江苏频道_活动快讯/会员之家内容配置表
public class ActivityAlertsController {
    @Autowired
    private ActivityAlertsService activityAlertsService;

    @RequestMapping("/members/list")
    public String membersList(HttpServletRequest request, Model model) {
        return activityAlertsList(request, model, ActivityAlerts.STATE_ONE);
    }

    /**
     * @Ceator:liuYI
     * @Describe:活动快讯列表
     */
    @RequestMapping("/list")
    public String activityAlertsList(HttpServletRequest request, Model model, String state) {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String type = request.getParameter("type");//0->协会快讯,1->重要活动 2->年度计划
        String deleteFlag = request.getParameter("deleteFlag");//0->正常,1->删除
        if (StringUtils.isBlank(state)) {
            state = request.getParameter("state");//0->活动快讯 1->会员之家
            if (StringUtils.isBlank(state)) {
                state = ActivityAlerts.STATE_ZERO;
            }
        }
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        ActivityAlertsQueryDTO activityAlertsQueryDTO = new ActivityAlertsQueryDTO();
        activityAlertsQueryDTO.setCurrentPage(currentPage);
        activityAlertsQueryDTO.setPageSize(pageSize);
        activityAlertsQueryDTO.setType(type);
        activityAlertsQueryDTO.setDeleteFlag(deleteFlag);
        activityAlertsQueryDTO.setState(state);
        PageModel<ActivityAlerts> page = activityAlertsService.queryActivityAlertsPage(activityAlertsQueryDTO);
        if (ActivityAlerts.STATE_ZERO.equals(state)) {
            model.addAttribute(Constants.MENU_NAME, Constants.ACTIVITY_ALERTS);
        } else {
            model.addAttribute(Constants.MENU_NAME, Constants.MEMBERS_OF_THE_FAMILY);
        }
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("page", page);
        model.addAttribute("dto", activityAlertsQueryDTO);
        model.addAttribute("state", state);
        return "/channel/activityAlerts/list";
    }

    /**
     * @Ceator:liuYI
     * @Describe:详情展示
     */
    @RequestMapping("/edit")
    public String activityAlertsEdit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        String state = request.getParameter("state");
        ActivityAlerts activityAlerts = new ActivityAlerts();
        if (StringUtils.isNotBlank(id)) {
            activityAlerts = activityAlertsService.find(id);
            if (activityAlerts != null) {
                state = activityAlerts.getState();
            }
        }
        if (ActivityAlerts.STATE_ZERO.equals(state)) {
            model.addAttribute(Constants.MENU_NAME, Constants.ACTIVITY_ALERTS);
        } else {
            model.addAttribute(Constants.MENU_NAME, Constants.MEMBERS_OF_THE_FAMILY);
        }
        model.addAttribute("activityAlerts", activityAlerts);
        model.addAttribute("state", state);
        return "/channel/activityAlerts/edit";
    }

    /**
     * @Ceator:liuYI
     * @Describe:活动快讯删除
     */
    @RequestMapping("/del")
    @ResponseBody
    public AjaxResult activityAlertsDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            if (StringUtils.isNotBlank(id)) {
                ActivityAlerts activityAlerts = activityAlertsService.find(id);
                String deleteFlag = activityAlerts.getDeleteFlag();
                deleteFlag = BaseEntity.DELETE_FLAG_NORMAL.equals(deleteFlag) ? BaseEntity.DELETE_FLAG_DELETED : BaseEntity.DELETE_FLAG_NORMAL;
                activityAlerts.setDeleteFlag(deleteFlag);
                activityAlertsService.update(activityAlerts);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    /**
     * @Ceator:liuYI
     * @Describe:置顶功能
     */
    @RequestMapping("/stick")
    @ResponseBody
    public AjaxResult stick(HttpServletRequest request) {
        String id = request.getParameter("id");
        ActivityAlerts activityAlerts = activityAlertsService.findMaxSort();
        ActivityAlerts result = activityAlertsService.find(id);
        int sort = activityAlerts.getSort();
        sort++;
        result.setSort(sort);
        activityAlertsService.update(result);
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    /**
     * @Ceator:liuYI
     * @Describe:后台添加/修改活动快讯
     */
    @RequestMapping("/save")
    @ResponseBody
    public AjaxResult activityAlertsSave(HttpServletRequest request, Model model) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        String state = request.getParameter("state");//0->活动快讯 1->会员之家
        try {
            String id = request.getParameter("id");
            String type = request.getParameter("type");//0->协会快讯,1->重要活动 2->年度计划
            String title = request.getParameter("title");//标题
            String author = request.getParameter("author");//作者
            String source = request.getParameter("source");//来源
            String introduction = request.getParameter("introduction");//导读
            String url = request.getParameter("url");//图片
            String file = request.getParameter("file");//file路径
            String content = request.getParameter("content");//内容
            String time = request.getParameter("time");////年读计划中使用的月份   例:2019-5
            if (StringUtils.isNotBlank(id)) {
                ActivityAlerts activityAlerts = activityAlertsService.find(id);
                activityAlerts.setType(type);
                activityAlerts.setTitle(title);
                activityAlerts.setAuthor(author);
                activityAlerts.setSource(source);
                activityAlerts.setIntroduction(introduction);
                activityAlerts.setUrl(url);
                activityAlerts.setFile(file);
                activityAlerts.setContent(content);
                if (StringUtils.isNotBlank(time)) {
                    activityAlerts.setTime(time);
                }
                activityAlerts.setState(state);
                activityAlertsService.update(activityAlerts);
            } else {
                ActivityAlerts activityAlerts = new ActivityAlerts();
                activityAlerts.setType(type);
                activityAlerts.setTitle(title);
                activityAlerts.setAuthor(author);
                activityAlerts.setSource(source);
                activityAlerts.setIntroduction(introduction);
                activityAlerts.setUrl(url);
                activityAlerts.setFile(file);
                activityAlerts.setContent(content);
                if (StringUtils.isNotBlank(time)) {
                    activityAlerts.setTime(time);
                }
                activityAlerts.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
                activityAlerts.setCreateDate(new Date());
                activityAlerts.setState(state);
                activityAlertsService.save(activityAlerts);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            model.addAttribute("state", state);
        }
        return ajaxResult;
    }

}
